package br.com.fiap.trabalho.dao;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

import br.com.fiap.trabalho.entity.Actor;
import br.com.fiap.trabalho.entity.Movie;

public class JPAActorDAO implements ActorDAO {

	public JPAActorDAO() {
		createEntityManager();
	}
	
	private EntityManager entityManager;

	private void createEntityManager() {
		EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("persistenceUnit"); 
		entityManager = entityManagerFactory.createEntityManager(); 
	}
	
	// Tempo: 5 minutos.
	public Actor createActor(Actor actor) {
		//createEntityManager();
		entityManager.getTransaction().begin();
		entityManager.persist(actor);
		entityManager.getTransaction().commit();
		//entityManager.close();
		return actor;
	}
	
	// Tempo: 8 minutos.
	public boolean deleteActor(Actor actor) {
		//createEntityManager();
		actor = this.findActor(actor.getId());
		if (actor == null) {
			return false;
		}
		entityManager.getTransaction().begin();
		entityManager.remove(actor);
		entityManager.getTransaction().commit();
		//entityManager.close();
		return true;
	}

	// Tempo: 15 minutos.
	@SuppressWarnings("unchecked")
	public List<Actor> selectActorByName(String name) {
		//createEntityManager();
		Query query = entityManager.createNativeQuery("select * from actor where fullname = :paramName",Actor.class);
		query.setParameter("paramName", name);
		List<Actor> listActorByName = query.getResultList();
		//entityManager.close();
		return listActorByName;
	}
	
	// Tempo: 20 minutos.
	@SuppressWarnings("unchecked")
	public List<Actor> selectActorByAge(int age) {
		SimpleDateFormat formatador = new SimpleDateFormat("yyyy-MM-dd");
		Calendar today = Calendar.getInstance();
		String yearOfBirth = Integer.toString((today.get(Calendar.YEAR)-age));
		java.util.Date startDate = new java.util.Date();
		java.util.Date finishDate = new java.util.Date();
		try {
			startDate = formatador.parse(yearOfBirth+"-01-01");
			finishDate = formatador.parse(yearOfBirth+"-12-31");
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		//createEntityManager();
		Query query = entityManager.createNativeQuery("select * from actor where birthdate between :paramStartDate and :paramFinishDate",Actor.class);
		query.setParameter("paramStartDate", startDate);
		query.setParameter("paramFinishDate", finishDate);
		List<Actor> listActorByAge = query.getResultList();
		//entityManager.close();
		return listActorByAge;
	}

	// Tempo: 15 minutos.
	@SuppressWarnings("unchecked")
	public List<Actor> selectActorByMovie(Movie movie) {
		//createEntityManager();
		Query query = entityManager.createNativeQuery("select a.id_actor, a.fullname from actor as a inner join movie_actor as ma on (a.id_actor = ma.id_actor) inner join movie as m on (ma.id_movie = m.id_movie) where m.id_movie = :paramIdMovie");
		query.setParameter("paramIdMovie", movie.getId());
		List<Actor> listActorByMovie = query.getResultList();
		//entityManager.close();
		return listActorByMovie;
	}
	
	private Actor findActor(int idActor){ 
		return entityManager.find(Actor.class, idActor); 
	} 
}
